/*
 /	This file is part of Answerkeys Disabler
 /
 /	Answerkeys Disabler is free software: you can redistribute it and/or modify
 /	it under the terms of the GNU General Public License as published by
 /	the Free Software Foundation, either version 3 of the License, or
 /	(at your option) any later version.
 /
 /	Answerkeys Disabler is distributed in the hope that it will be useful,
 /	but WITHOUT ANY WARRANTY; without even the implied warranty of
 /	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 /	GNU General Public License for more details.
 /
 /	You should have received a copy of the GNU General Public License
 /	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 /
 /	Copyright 2008 Daan van Dijk (drvdijk@gmail.com)
 */

#include "setupdll.h"

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
	switch(ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			break;
	}
	return TRUE;
}


SETUPDLL_API codeINSTALL_INIT Install_Init(HWND hwndParent, BOOL fFirstCall, BOOL fPreviouslyInstalled, LPCTSTR pszInstallDir)
{

	return codeINSTALL_INIT_CONTINUE;

}


SETUPDLL_API codeINSTALL_EXIT Install_Exit(HWND hwndParent, LPCTSTR pszInstallDir, WORD cFailedDirs, WORD cFailedFiles, WORD cFailedRegKeys, WORD cFailedRegVals, WORD cFailedShortcuts)
{
	if(cFailedDirs == 0
	&& cFailedFiles == 0
	&& cFailedRegKeys == 0
	&& cFailedRegVals == 0
	&& cFailedShortcuts == 0)
	{

		PROCESS_INFORMATION info;

		TCHAR programFolderPath[MAX_PATH];
		SHGetSpecialFolderPath(NULL, programFolderPath, CSIDL_PROGRAM_FILES, 0);
		TCHAR completePath[MAX_PATH] = {'\0'};
		swprintf(completePath, L"%s\\Answerkeys Disabler\\AnswerkeysDisabler.exe", programFolderPath);

		if(!CreateProcess(completePath, NULL, NULL, NULL, FALSE, INHERIT_CALLER_PRIORITY, NULL, NULL, NULL, &info)) {
			MessageBox(hwndParent, _T("Answerkeys Disabler has not started yet, please start it manually"), _T("Answerkeys Disabler"), MB_OK | MB_ICONINFORMATION | MB_TOPMOST);
		}
		return codeINSTALL_EXIT_DONE;
	}

	return codeINSTALL_EXIT_UNINSTALL;
}


codeUNINSTALL_INIT Uninstall_Init(HWND hwndParent, LPCTSTR pszInstallDir)
{

	PROCESS_INFORMATION info;

	TCHAR programFolderPath[MAX_PATH];
	SHGetSpecialFolderPath(NULL, programFolderPath, CSIDL_PROGRAM_FILES, 0);
	TCHAR completePath[MAX_PATH] = {'\0'};
	swprintf(completePath, L"%s\\Answerkeys Disabler\\AnswerkeysDisabler.exe", programFolderPath);

	if(CreateProcess(completePath, _T("/u"), NULL, NULL, FALSE, INHERIT_CALLER_PRIORITY, NULL, NULL, NULL, &info))
	{
		WaitForSingleObject(info.hProcess, INFINITE);
	}
	else
	{
		MessageBox(hwndParent, _T("Failed to disable Answerkeys Disabler"), _T("Error"), MB_OK | MB_ICONERROR | MB_TOPMOST);
		//return codeUNINSTALL_INIT_CANCEL;
	}

	return codeUNINSTALL_INIT_CONTINUE;
}


codeUNINSTALL_EXIT Uninstall_Exit(HWND hwndParent)
{
	return codeUNINSTALL_EXIT_DONE;
}
